package com.ecube.mqtt.page.listener.chargelistener;

import com.ecube.mqtt.service.ChargeSendService;
import com.ecube.mqtt.utils.CommonConstant;
import com.ecube.mqtt.utils.PropertyUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ChargeAuthListener implements ActionListener {
    private JComboBox env;
    private JTextField stationId;
    private JTextField gunid;
    private JComboBox guncode;

    private JTextField soctext;
    private JTextField vehicletext;
    private JFrame start;
    private String path;
    public ChargeAuthListener(JFrame start,
                              JComboBox env,
                              JTextField stationId,
                              JTextField gunid,
                              JComboBox guncode,
                              JTextField vehicletext,
                              JTextField soctext,
                              String path) {
        this.start = start;
        this.env = env;
        this.stationId = stationId;
        this.gunid = gunid;
        this.guncode = guncode;
        this.soctext = soctext;
        this.vehicletext = vehicletext;
        this.path = path;
    }

    public void actionPerformed(ActionEvent e) {
        Dimension dim2 = new Dimension(100, 30);
        Dimension dim3 = new Dimension(300, 30);

        Map propertys = new HashMap();

        propertys.put("env",env.getSelectedItem().toString());
        propertys.put("stationId",stationId.getText());
        propertys.put("gunid",gunid.getText());
        propertys.put("guncode",guncode.getSelectedItem().toString());
        propertys.put("vehicle",vehicletext.getText());
        propertys.put("soc",soctext.getText());
        try {
            PropertyUtil.setProperty(propertys, CommonConstant.CHARGE_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //生成新界面
        JFrame startres = new JFrame();
        startres.setSize(400, 200);
        startres.setTitle("发送结果");//设置窗体标题
        startres.setDefaultCloseOperation(2);
        startres.setLocationRelativeTo(null);
        startres.setFont(new Font("宋体", Font.PLAIN, 14));  //宋体，正常风格，14号字体

        FlowLayout fl = new FlowLayout(FlowLayout.CENTER,30,30);
        //实例化流式布局类的对象
        startres.setLayout(fl);

        //创建组件
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        ChargeSendService chargeSendService = new ChargeSendService();
        Boolean startresult = false;
        String gunno = "";
        if(guncode.getSelectedItem().toString().equals("A枪")){
            gunno = "1";
        }else if (guncode.getSelectedItem().toString().equals("B枪")){
            gunno = "2";
        }else if (guncode.getSelectedItem().toString().equals("AB枪")){
            gunno = "12";
        }
        try {
            startresult = chargeSendService.auth(env.getSelectedItem().toString(), stationId.getText(), vehicletext.getText(), gunid.getText(), gunno);
        } catch (InterruptedException ex) {
            System.out.println("鉴权请求失败");
            throw new RuntimeException(ex);
        }

        if (startresult) {
            JLabel message = new JLabel("鉴权发送成功");
            message.setFont(new Font("宋体", Font.PLAIN, 20));  //宋体，正常风格，14号字体
            message.setPreferredSize(dim3);
            jp1.add(message);
            startres.add(jp1, BorderLayout.CENTER);

            JButton close = new JButton("确定");
            close.setFont(new Font("宋体", Font.PLAIN, 18));
            //设置按键大小
            close.setSize(dim3);
            jp2.add(close);
            startres.add(jp2, BorderLayout.SOUTH);

            close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    startres.dispose();
                }
            });

            startres.setResizable(false);
            startres.setVisible(true);

        } else {
            JLabel message = new JLabel("鉴权发送失败");
            message.setFont(new Font("宋体", Font.PLAIN, 20));  //宋体，正常风格，14号字体
            message.setPreferredSize(dim3);
            //将textName标签添加到窗体上
            jp1.add(message);
            startres.add(jp1, BorderLayout.CENTER);

            JButton close = new JButton("确定");
            close.setFont(new Font("宋体", Font.PLAIN, 18));
            //设置按键大小
            close.setSize(dim3);
            jp2.add(close);
            startres.add(jp2, BorderLayout.SOUTH);

            close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    startres.dispose();
                }
            });

            startres.setResizable(false);
            startres.setVisible(true);

        }
    }
}